# 作者：SP
# 开发时间：2025/3/30 21:26
import sys
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtWidgets import *
class MyDialog(QDialog):
    def __init__(self):
        super().__init__()
    #重新实现按键处理函数keyPressEvent
    '''
    QMessageBox.information(
    parent,         # 父窗口（通常是 self）
    "标题",        # 对话框标题
    "消息内容",    # 对话框显示的消息
    QMessageBox.Yes # 默认按钮（如 QMessageBox.Yes / No / Ok / Cancel）)'''
    def keyPressEvent(self, event):
        QMessageBox.information(self,'信息提示框','in keyPressEvent',QMessageBox.Yes)
        QDialog.keyPressEvent(self,event)
    #重新实现event
    def event(self,event):
        if event.type() == QKeyEvent.KeyPress:
            QMessageBox.information(self,'信息提示框','让keyPressEvent得不到执行',QMessageBox.Cancle)
            #QDialog.event(self,event)
            return True
        return QDialog.event(self,event)   #mask sure the rest of event are handled

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywindow = MyDialog()
    mywindow.exec_()
    sys.exit(app.exec_())
